import $http from '../../libs/axios.js';
import { MessageBox } from 'element-ui';
export default {
    namespaced: true,
    state: {
        currentUser: {
            UserToken: '',
            RefreshToken: '',
            addressInfo: '',
            userData: ''
        },
        cartNum: '',
        messageNum: '',
        reg: {
            passwordPattern: "/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?![,\\.#%'\\+\\*\\-:;^_`]+$)[,\\.#%'\\+\\*\\-:;^_`0-9A-Za-z]{8,20}$/", //8~20位,数字、英文、符号至少两种组合的字符
            phoneNumberPattern: "/^1(3|4|5|7|8)\\d{9}$/", //手机号格式
            userNamePattern: "/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/", //6至20位，以字母开头，字母、数字、减号、下划线
            telPattern: "/^(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{7,14}$/",//固话格式
        },
        order: {
            list: '',
            addressInfo: '',
            amount: '',
            freight: '',
            invoice: '',
            voucher: '',
        },
        orderTotal: ''
    },
    mutations: {
        setUser(state, { user_name, user_token, refresh_token }) {
            // 在这里把token保存起来
            state.currentUser.UserToken = user_token;
            state.currentUser.RefreshToken = refresh_token;
        },
        clearUser(state) {
            state.currentUser = {
                UserToken: '',
                RefreshToken: '',
                addressInfo: '',
                userData: ''

            };
            state.cartNum = '';
            state.messageNum = '';
        },
        updateUserData(state, data) {
            state.currentUser.userData = data;
        }
    },
    actions: {
        userLogin(context, { user_name, user_password, router, path }) {
            $http.post('/oauth/token', null, {
                params: {
                    "username": user_name,
                    "password": user_password,
                    "grant_type": "password",
                    "scope": "all"
                },
                auth: {
                    username: 'client',
                    password: 'secret'
                },
                withCredentials: true
            }).then(res => {
                context.commit('setUser', { 'user_name': user_name, 'user_token': res.data.access_token, 'refresh_token': res.data.refresh_token });
                context.commit('setSignTrue', null, { root: true });
                context.dispatch('getUserData');//获取user
                context.dispatch('getAddressInfo');//获取address
                context.dispatch('getOrderTotal');//获取订单信息
                context.dispatch('getCartNum');
                if (path) router.push(decodeURIComponent(path));
                else router.push('/');
            }).catch(error => {
                // todo登陆失败
                MessageBox.alert('登录错误，请重新输入！');
                context.commit('setSignFalse', null, { root: true });
            })
        },
        userLogout(context, { router }) {
            $http.post('public/logout', null, { showLoading: false });
            router.push({ path: '/login' });
            location.reload();
            context.commit('clearUser');
            context.commit('clearOrder');
            context.commit('updateOrderTotal');
            context.commit('setSignFalse', null, { root: true });
        },
    }
}